package com.gameapplet.ranklist.controller;

import com.alibaba.fastjson.JSONObject;
import com.gameapplet.ranklist.model.RankList;
import com.gameapplet.ranklist.service.IRankListService;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

import java.util.Map;


@Controller
@RequestMapping("/ranklist")
public class RankListController {
    @Autowired
    private HttpServletRequest request;
    @Resource(name="iRankListService")
    IRankListService iRankListService;
    @RequestMapping("/getranklist")
    @ResponseBody
    public Map countryranklist(@RequestBody String openId){
        JSONObject ResultJson =JSONObject.parseObject(openId);
        String userid = ResultJson.getString("openId");
        int i= 1;
        //总排行榜信息
        List<RankList> list = iRankListService.selectRankList();//
        Map  map = new HashMap();
        //用户排行榜信息
        for (RankList li : list){
            li.setNum(i++);
            map.put("cuntryranklist",list);
            if(li.getOpenid()==userid){
                map.put("userranklist",li);
            }
        }
        return map;
    }
    @RequestMapping("/getgrouprank")
    @ResponseBody
    public Map getgrouprank(@RequestBody Map map){
        String opengid = map.get("openGid").toString();
//        String openid = map.get("openId").toString();
        int i =1;
        List<RankList> list = iRankListService.selectGroupRank(opengid);
        for (RankList li : list) {
            li.setNum(i++);
           map.put("groupranklist", list);
//            if(li.getOpenid()==openid){
    //                map.put("userrank",li);
//            }
        }
        return map;
    }

    @RequestMapping("/getstagerank")
    @ResponseBody
    public Map getgstagerank(@RequestBody Map m){
        String stageid = m.get("stageid").toString();
        String openid = m.get("openid").toString();
        Map map = new HashMap();
        int i = 1;
        List<RankList> list = iRankListService.selectStageTenRankList(stageid);
        List<RankList> list2 = new LinkedList();
        for (RankList li : list) {
            li.setNum(i++);
            map.put("groupranklist", list);//全部
            if (li.getOpenid().equals(openid)){
                map.put("userranklist", li);//本人排行
            }
        }
        if (list.size() < 10) {
            list2 = list.subList(0, list.size());
            map.put("sublist", list2);
        } else {
            list2 = list.subList(0, 10);
            map.put("sublist", list2);//取本关前十
        }
        return  map;
    }
}
